Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
PurgeCSS is a tool to remove unused CSS. It can be used as part of your development workflow to reduce the size of your CSS files by removing unused selectors.
Basic Usage
This code demonstrates the basic usage of PurgeCSS. It scans HTML files and CSS files to remove unused CSS selectors.
const PurgeCSS = require('purgecss');
const purgeCSSResults = new PurgeCSS().purge({
content: ['**/*.html'],
css: ['**/*.css']
});
console.log(purgeCSSResults);
Using with PostCSS
This code shows how to integrate PurgeCSS with PostCSS. It processes CSS files and removes unused selectors based on the content of HTML files.
const postcss = require('postcss');
const purgecss = require('@fullhuman/postcss-purgecss');
postcss([
purgecss({
content: ['./src/**/*.html']
})
]).process(css, { from: 'src/app.css', to: 'dist/app.css' })
.then(result => {
console.log(result.css);
});
Using with Webpack
This code demonstrates how to use PurgeCSS as a Webpack plugin. It scans the specified paths for content and removes unused CSS selectors during the Webpack build process.
const PurgeCSSPlugin = require('purgecss-webpack-plugin');
const glob = require('glob');
const path = require('path');
module.exports = {
// other webpack config
plugins: [
new PurgeCSSPlugin({
paths: glob.sync(`${path.join(__dirname, 'src')}/**/*`, { nodir: true }),
}),
],
};
UnCSS is a tool that removes unused CSS from your stylesheets. It works similarly to PurgeCSS by scanning your HTML files to determine which CSS selectors are not used. However, UnCSS is generally considered to be less flexible and slower compared to PurgeCSS.
PurifyCSS is another tool for removing unused CSS. It scans your HTML and JavaScript files to find which CSS selectors are used. While it is similar to PurgeCSS, it is not as actively maintained and may lack some of the advanced features and integrations that PurgeCSS offers.
CSSO (CSS Optimizer) is a CSS minifier that also has the capability to remove unused CSS. It is primarily focused on minification and optimization, but it can also be used to remove unused CSS selectors. CSSO is generally faster but may not be as thorough in removing unused CSS as PurgeCSS.
When you are building a website, chances are that you are using a css framework like Bootstrap, Materializecss, Foundation, etc... But you will only use a small set of the framework and a lot of unused css styles will be included.
This is where PurgeCSS comes into play. PurgeCSS analyzes your content and your css files. Then it matches the selectors used in your files with the one in your content files. It removes unused selectors from your css, resulting in smaller css files.
You can find the PurgeCSS documentation on this website.
npm i --save-dev purgecss
import Purgecss from 'purgecss'
const purgeCss = new Purgecss({
content: ['**/*.html'],
css: ['**/*.css']
})
const result = purgeCss.purge()
With a custom extractor:
import Purgecss from 'purgecss'
import purgeHtml from 'purgecss-from-html'
const purgeCss = new Purgecss({
content: ['**/*.html'],
css: ['**/*.css'],
extractors: [
{
extractor: purgeHtml,
extensions: ['html']
}
]
})
const result = purgeCss.purge()
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
PurgeCSS use SemVer for versioning.
PurgeCSS was originally thought as the v2 of purifycss. And because of it, it is
greatly inspired by it.
Some of the plugins such as purgecss-webpack-plugin are based on the purifycss plugin.
Below is the list of the purifycss repositories:
This project is licensed under the MIT License - see the LICENSE file for details.
The extractors needs to be defined from the more specific to the less specific.
Meaning that you need to define js
extractor after ejs
. So the js
extractor will not be selected for ejs files.
You can specified extensions like
.es.js
.
FAQs
Remove unused css selectors
The npm package purgecss receives a total of 689,682 weekly downloads. As such, purgecss popularity was classified as popular.
We found that purgecss demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.